﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;



namespace HaGisPack
{
   public  class UISystem:SubSystem 
    {
       public UISystem(GisManager p)
           : base(p)
       {

           this.Parent.LayerSys.LayerChanged += new GisHandler(LayerSys_LayerChanged);          
 
       }


       void LayerSys_LayerChanged()
       {
           if (m_Tree ==null )
           {
               return;
               
           }


           this.RefreshTreeView();

           this.PropertyGrid.SelectedObject = null;
           
       }



       protected TreeView m_Tree;

       public  TreeView Tree
       {
           get { return m_Tree; }
           set { m_Tree = value;

           this.OnSetTreeView();

           }
       }

       protected void OnSetTreeView()
       {

           this.ModifyTreeView();
           this.RefreshTreeView();

       }

       protected PropertyGrid m_PropertyGrid;

       public PropertyGrid PropertyGrid
       {
           get { return m_PropertyGrid; }
           set
           {
               m_PropertyGrid = value;
               this.OnSetPropertyGrid();

           }
       }


       protected void OnSetPropertyGrid()
       {

 
       }


       protected TreeNode m_dragNode;


       #region 树形控件的配置

       /// <summary>
       /// 通过订阅树形控件的事件，来实现自己的目的
       /// </summary>
       protected void ModifyTreeView()
       {

           //指定外观
           m_Tree.CheckBoxes = true;
           m_Tree.AllowDrop = true;
           

           //添加事件对应


           m_Tree.AfterCheck += new TreeViewEventHandler(m_Tree_AfterCheck);
           m_Tree.AfterSelect += new TreeViewEventHandler(m_Tree_AfterSelect);

           //最大化显示

           m_Tree.NodeMouseClick += new TreeNodeMouseClickEventHandler(m_Tree_NodeMouseClick);
           m_Tree.NodeMouseDoubleClick += new TreeNodeMouseClickEventHandler(m_Tree_NodeMouseDoubleClick);

           //移动图层相关

           m_Tree.ItemDrag += new ItemDragEventHandler(m_Tree_ItemDrag);
           m_Tree.DragDrop += new DragEventHandler(m_Tree_DragDrop);
           m_Tree.DragEnter += new DragEventHandler(m_Tree_DragEnter);

           //m_Tree.DragDrop += new ItemDragEventHandler(this.m_Tree_DragDrop);
           //m_Tree.DragEnter += new ItemDragEventHandler(m_Tree_DragEnter);
 
       }




       void m_Tree_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
       {
           //this.m_Tree.SelectedNode = e.Node;

           int index = e.Node.Index;

           DotSpatial.Symbology.ILayer ilayer = this.m_Parent.CurrentMap.MapFrame.Layers[index];

           try
           {
               this.m_Parent.CurrentMap.MapFrame.ViewExtents = ilayer.Extent;
           }
           catch (Exception ex)
           { }
          
       }

    

       

       void m_Tree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
       {
           //这里包含很多东西，比如右键菜单的显示，选择项的确定

           this.m_Tree.SelectedNode = e.Node;


           //右键菜单模型还没有设计






       }

       void m_Tree_DragEnter(object sender, DragEventArgs e)
       {

           if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode"))
           {
               e.Effect = DragDropEffects.Move;
           }
           else
           {
               e.Effect = DragDropEffects.None;
           }

       }

       void m_Tree_DragDrop(object sender, DragEventArgs e)
       {
           if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode"))
           {
               System.Drawing.Point p = this.Tree.PointToClient(new System.Drawing.Point(e.X, e.Y));

               TreeNode dragNode = Tree.GetNodeAt(p.X, p.Y);

               if (dragNode == null) return;


               //插入拖动的项

               int insertIndex = dragNode.Index;  //插入位置

               TreeNode insertNode = this.m_dragNode.Clone() as TreeNode;

               int startIndex = this.m_dragNode.Index;  //起始位置

               if (insertIndex == startIndex) return;

               DotSpatial.Controls.IMapLayer ilayer = this.m_Parent.CurrentMap.MapFrame.Layers[startIndex];

               try
               {
                  
                   //ilayer.Projection = DotSpatial.Projections.KnownCoordinateSystems.Geographic.World.WGS1984;

                   // 必须要加LockDispose与UnlockDispose 这两个方法,不知道是什么意思呢
                   ilayer.LockDispose();

                   this.m_Parent.CurrentMap.MapFrame.RemoveAt(startIndex);  //先移除

                   this.m_Parent.CurrentMap.MapFrame.Insert(insertIndex, ilayer); //再添加进去

                   ilayer.UnlockDispose();

                   

                   if (insertIndex > startIndex)
                   {
                       Tree.Nodes.Insert(insertIndex + 1, insertNode);
                   }
                   else
                   {
                       Tree.Nodes.Insert(insertIndex, insertNode);
                   }

                   this.m_dragNode.Remove();
               }
               catch (Exception ex)
               {
                   MessageBox.Show("Sorry,图层移动失败了!");
               }



           }
           else
           { 
           }
       }

       void m_Tree_ItemDrag(object sender, ItemDragEventArgs e)
       {
           this.m_dragNode = e.Item as TreeNode;

           if (e.Button == MouseButtons.Left && this.m_dragNode != null)
           {
               this.Tree.DoDragDrop(this.m_dragNode, DragDropEffects.Copy | DragDropEffects.Move | DragDropEffects.Link);
               //this.Tree.DoDragDrop(this.m_dragNode, DragDropEffects.Move);
           }
       }

     

       void m_Tree_AfterSelect(object sender, TreeViewEventArgs e)
       {
           

           string lname = e.Node.Name;
           FLayer layer = this.Parent.LayerSys.GetLayer(lname);
                      

           if (layer != null)
           {

               this.PropertyGrid.SelectedObject = layer;
           }


       }

       void m_Tree_AfterCheck(object sender, TreeViewEventArgs e)
       {

           string lname = e.Node.Name;
           FLayer layer = this.Parent.LayerSys.GetLayer(lname);

           if (layer != null)
           {
               if (layer.LayerType == FLayerType.CompoundLayer)
               {
                   foreach (TreeNode node in e.Node.Nodes)
                   {
                       node.Checked = e.Node.Checked;
                   }

                   foreach (FLayer lay in (layer as FCompoundLayer).Childs.Values)
                   {
                       lay.IsVisible = e.Node.Checked;
                   }
               }
               else
               {
                   layer.IsVisible = e.Node.Checked;
               }

           }

          
       }



       /// <summary>
       /// 根据图层的类重绘图层的树形控件
       /// </summary>
       public void RefreshTreeView()
       {
           if (m_Tree == null)
           {
               return;

           }

           if (this.Parent .LayerSys .Layers ==null )
           {
               this.Tree.Nodes.Clear();
               return;

           }


           //第一步忘记清空了
           this.Tree.Nodes.Clear();

           //根据图形的图层，开始绘制



           //目前只支持一级复合层

           foreach (FLayer  item in this.Parent .LayerSys .Layers.Values  )
           {
               string key = item.Id;
               string desc = item.descString;


               //加入对应的节点
               TreeNode  node = new TreeNode(desc);
               node.Name = key;
               node.Checked = item.IsVisible;

               this.m_Tree.Nodes.Add(node);



               ///如果是复合类型的话
               if (item.LayerType == FLayerType.CompoundLayer)
               {
                   FCompoundLayer cl=item as FCompoundLayer ;

                   if (cl!=null )
	                {
                       
                       foreach (FLayer ftemp in cl.Childs .Values )
                       {
                           string keytemp = ftemp.Id;

                           string desctemp = ftemp.descString;

                           TreeNode  nodetemp = new TreeNode(desctemp );
                           nodetemp.Name = keytemp;
                           nodetemp.Checked = ftemp.IsVisible;

                           node.Nodes.Add(nodetemp );

                       
                       }
		 
	                }

               }
               else
               {

 
               }
               
           }



       }


      


       #endregion




    }
}
